package com.manman.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.manman.commonUtils.Result;
import com.manman.serviceBase.exception.GuException;
import com.manman.vod.service.VodService;
import com.manman.vod.utils.ConstantVodUtils;
import com.manman.vod.utils.InitVodClient;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduVod/video")
@CrossOrigin
public class VodController {

    @Autowired
    VodService vodService;

    // 上传视频到阿里云
    @PostMapping("uploadAlyVideo")
    public Result uploadAlyVideo(MultipartFile file){
        // 返回视频 Id
        String videoId = vodService.uploadAlyVideo(file);
        return Result.ok().data("videoId", videoId);
    }

    // 根据视频 id 删除阿里云视频
    @DeleteMapping("removeAlyVideo/{id}")
    public Result removeAlyVideo(@PathVariable String id){
        try{
            // 初始化的ui下
            DefaultAcsClient client = InitVodClient.initVoClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
            // 创建删除视频 request 对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 向 request 设置视频 id
            request.setVideoIds(id);
            // 调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            throw new GuException(20001, "删除视频失败");
        }
    }

    // 删除多个视频
    // 参数是多个视频 id
    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestParam("videoIdList") List videoIdList){
        vodService.removeMoreAlyVideo(videoIdList);
        return Result.ok();
    }
}
